from rest_framework.exceptions import PermissionDenied
from rest_framework.views import exception_handler


def custom_exception_handler(exc, context):
    # 调用默认的异常处理器
    response = exception_handler(exc, context)
    if isinstance(exc, PermissionDenied):
        # 如果异常是 PermissionDenied，修改响应体
        if response is not None:
            response.data['msg'] = response.data.pop('detail', '未授权的访问')
    return response
